package com.wef.system.service.impl;

import java.util.List;
import java.util.Set;

import com.gfa4j.exception.ValidateRuntimeException;
import com.gfa4j.hibernate.BaseObject;
import com.gfa4j.service.impl.BaseServiceImpl;
import com.wef.system.entity.Office;
import com.wef.system.entity.Role;
import com.wef.system.entity.domain.OfficeDomain;
import com.wef.system.service.OfficeService;

public class OfficeServiceImpl extends BaseServiceImpl implements OfficeService {

	public Office addOffice(Office office){
		
		return (Office) super.saveObject(office);
	}

	public void deleteOffice(Office office) {		
		super.deleteObject(office);
		
	}

	public Office saveOrUpdateOffice(Office office){	
		if (office.getRowStatus() == BaseObject.ROWSTATUS_ADDED) {
			office.setDeleted(false);
			return this.addOffice(office);
		}
		if (office.getRowStatus() == BaseObject.ROWSTATUS_MODIFIED) {
			office.setDeleted(false);
			return this.updateOffice(office);
		}
		if (office.getRowStatus() == BaseObject.ROWSTATUS_DELETED) {
			
			this.deleteOffice(office);
			return null ;
		}
		return office;
	}

	public int officeCodeCount(String officeCode)
	{
		return this.findCountByProperty(Office.class, "officeCode", officeCode); 
	}
	
	public Set<Office> saveOrUpdateOffices(Set<Office> offices){
		StringBuilder errorString = new StringBuilder();
		for (Office office : offices) {
			try {
				saveOrUpdateOffice(office);
			} catch (ValidateRuntimeException ex) {
				errorString.append(ex.getMessage() + "\n");
			}
		}
		if (errorString.length() > 0) {
			throw new ValidateRuntimeException(errorString.toString());
		}
		return offices;
	}

	public Office updateOffice(Office office) {
		return (Office) super.updateObject(office);
	}	
	
	public List<Office> getOfficeTreeAll(){//����isdeleted==0�����
		List<Office> loadAllByActive = this.findByProperty(Office.class, OfficeDomain.deleted, false);
		return loadAllByActive;
	}
}
